package com.cgw.game.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @类描述 渐变色进度条
 * @作者 YF
 * @创建时间 2020/4/17 11:45
 */
public class ScoreProgress extends View {
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;

    /**
     * 字体背景画笔
     * */
    private Paint mTextBgPaint;
    /**
     * 字体画笔
     * */
    private Paint mTextPaint;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;

    /**
     * 字大小
     * */
    private float mTextSize = 40f;

    /**
     * 上下文对象
     * */
    private Context context;

    /**
     * 左右默认边距
     * */
    private float defaultMargin = 48f;

    private float strokeWidth = 30f;


    private float progress = 100f;
    private float max = 100;

    public ScoreProgress(Context context) {
        this(context,null);
    }

    public ScoreProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ScoreProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setStyle(Paint.Style.STROKE);
        mBgPaint.setStrokeCap(Paint.Cap.ROUND);
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(Color.parseColor("#5f88f0"));

        mTextBgPaint = new Paint();
        mTextBgPaint.setAntiAlias(true);
        mTextBgPaint.setColor(Color.parseColor("#1e5f88f0"));
        mTextBgPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        width = measureSize(widthMeasureSpec);
        height = measureSize(heightMeasureSpec);
        setMeasuredDimension(width,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,60);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBackGround(canvas);
        drawProgress(canvas);
    }
    int[] colors = new int[]{
            Color.parseColor("#F95767"),
            Color.parseColor("#FA9269"),
            Color.parseColor("#FFAC4D"),
            Color.parseColor("#FFEE6D"),
            Color.parseColor("#7EEED5"),
            Color.parseColor("#497DFF"),
    };
    float[] positions = new float[]{
            0f,
            0.2f,
            0.39f,
            0.59f,
            0.79f,
            1.0f,
    };
    //画背景
    private void drawBackGround(Canvas canvas){
        mBgPaint.setStrokeWidth(strokeWidth);
        LinearGradient linear = new LinearGradient(defaultMargin,0,width-defaultMargin,0,colors,positions, Shader.TileMode.CLAMP);
        mBgPaint.setShader(linear);
        canvas.drawLine(defaultMargin,height-strokeWidth,width-defaultMargin,height-strokeWidth,mBgPaint);
    }

    //画进度
    private void drawProgress(Canvas canvas){
        float step = (width-(2*defaultMargin))/max;

        //画圆点
        mBgPaint.setShader(null);
        mBgPaint.setColor(Color.WHITE);
        mBgPaint.setStrokeWidth(strokeWidth+15);
        canvas.drawPoint((step*progress)+defaultMargin,height-strokeWidth,mBgPaint);
        mBgPaint.setStrokeWidth(strokeWidth);
        mBgPaint.setColor(Color.parseColor("#5f88f0"));
        canvas.drawPoint((step*progress)+defaultMargin,height-strokeWidth,mBgPaint);

        float currentX= (step*progress)+defaultMargin;
        float currentY= height-2f*strokeWidth;

        float  triangle = DensityUtil.dip2px(context,6); //小三角的x y偏移量
        float  rectangle = DensityUtil.dip2px(context,28); //矩形的X Y偏移量


        Path path = new Path();
        path.moveTo(currentX, currentY);
        path.lineTo(currentX-triangle, currentY-triangle);
        path.lineTo(currentX+triangle, currentY-triangle);
        path.lineTo(currentX, currentY);
        path.close();
        canvas.drawPath(path,mTextBgPaint);
        float left = 0;
        float right = 0;
        if(currentX-rectangle<0){
            left = 0-(currentX-rectangle)+10f;  //10f防止贴边
        }
        if(currentX+rectangle>width){
            right = (currentX+rectangle)-width*1f +10f;
        }
        canvas.drawRoundRect(currentX-rectangle+left-right,currentY-rectangle,currentX+rectangle+left-right,currentY-triangle,15,15,mTextBgPaint);

        String text = String.format("%.2f" ,progress)+"分";
        float textWidth = mTextPaint.measureText(text);
        Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
        float textHeight = metrics.ascent-metrics.descent;

        canvas.drawText(text,currentX-textWidth/2+left-right, currentY-(rectangle+triangle)/2-textHeight/3,mTextPaint);
    }


    public void setProgress(float progress) {
        startAnimator(progress);
    }

    public void setMax(float max) {
        this.max = max;
    }
    ValueAnimator animator;
    private void startAnimator(float pro){
        if(animator!=null&&animator.isRunning()){
            animator.cancel();
            animator = null;
        }
        animator = ValueAnimator.ofFloat(pro);
        animator.setDuration(1500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                progress = (float)animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}
